package com.itheima.EX;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;

//import javax.swing.text.Document;

public class BiQuGe {
    public static void main(String[] args) throws Exception {
        String url = "http://www.xbiquge.la/10/10489/4535761.html";


        FileOutputStream out = new FileOutputStream("b.txt");

        //缓冲流需要添加刷新flush
        BufferedWriter bw= new BufferedWriter(new FileWriter("a.txt", true));
        while (true) {

            Document document = BiQuGe.getdocument(url);
            Elements zj = document.select(".bookname h1");
            String zjname = zj.text();
            System.out.println(zjname);

            Elements content = document.select("#content");

            String text = content.text();
            System.out.println(text);


//
            out.write(text.getBytes());

            bw.write(text);
            bw.flush();

            Elements nextUrl = document.select("#wrapper > div.content_read > div > div.bookname > div.bottem1 > a:nth-child(4)");
            url = nextUrl.attr("href");

            url = "http://www.xbiquge.la" + url;
            System.out.println(url);
            if (url.equals("http://www.xbiquge.la/10/10489/9687867.html")) {
                break;

            }

        }
    }

    private static Document getdocument(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");

        CloseableHttpResponse response = httpClient.execute(httpGet);

        HttpEntity entity = response.getEntity();
        String html = EntityUtils.toString(entity, "utf-8");
//
//        org.jsoup.nodes.Document document = Jsoup.parse(html);
        Document document = Jsoup.parse(html);
        httpClient.close();

        return document;
    }
}
